package com.e2oto.seller.modules.seller.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.utils.SPtools;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.seller.bean.ApplyAdmissionCommitBean;
import com.e2oto.seller.modules.seller.bean.FirstSystemCategoryBean;
import com.e2oto.seller.modules.seller.bean.RxBusBean;
import com.e2oto.seller.modules.seller.bean.SellerTypeBean;
import com.e2oto.seller.modules.seller.presenter.ShopBaseInfoPresenter;
import com.e2oto.seller.modules.seller.views.ShopBaseInfoView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZhangLiQiang on 2017/11/14.
 */

public class ShopBaseInfoActivity extends BaseMvpActivity<ShopBaseInfoView, ShopBaseInfoPresenter> implements ShopBaseInfoView {

    @BindView(R.id.shop_base_info_shop_name)
    EditText shopNameEt;
    @BindView(R.id.shop_base_info_spokesman_name)
    EditText spokesmanEt;
    @BindView(R.id.shop_base_info_seller_type)
    Spinner sellerTypeSp;
    @BindView(R.id.shop_base_info_retail_system_category)
    Spinner retailSystemCategorySp;
    @BindView(R.id.shop_base_info_service_region)
    TextView serviceRegionTv;
    @BindView(R.id.shop_base_info_is_retail)
    CheckBox isRetailCb;
    @BindView(R.id.shop_base_info_is_wholesale)
    CheckBox isWholesaleCb;
    @BindView(R.id.shop_base_info_batch_system_category)
    Spinner batchSystemCategorySp;
    @BindView(R.id.shop_base_info_retail_region)
    LinearLayout retailRegion;
    @BindView(R.id.shop_base_info_batch_region)
    LinearLayout batchRegion;

    private List<SellerTypeBean> sellerTypes;
    private List<FirstSystemCategoryBean.RetailBean> retailBeanList;
    private List<FirstSystemCategoryBean.BatchBean> batchBeanList;
    private ApplyAdmissionCommitBean commitBean = new ApplyAdmissionCommitBean();

    @Override
    protected ShopBaseInfoPresenter createPresenter() {
        return new ShopBaseInfoPresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_shop_base_info;
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
        mPresenter.getSellertype();
        mPresenter.getFirstSystemCategory();
        serviceRegionTv.setText(SPtools.getString(this, "province", "") + SPtools.getString(this, "city", "") + SPtools.getString(this, "district", ""));
        isRetailCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    retailRegion.setVisibility(View.VISIBLE);
                } else {
                    retailRegion.setVisibility(View.GONE);
                }
            }
        });
        isWholesaleCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    batchRegion.setVisibility(View.VISIBLE);
                } else {
                    batchRegion.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void onReceiveRxMessage(RxBusBean anyType) {
        super.onReceiveRxMessage(anyType);
        if (anyType.getType() == 421) {
            commitBean.setArea_tree(anyType.getMsg());
            serviceRegionTv.setText(commitBean.getArea_tree());
        }
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @Override
    public void getSellerTypeCallback(List<SellerTypeBean> list) {
        sellerTypes = list;
        ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sellerTypeSp.setAdapter(arrayAdapter);
    }

    @Override
    public void getFirstSystemCategoryCallback(FirstSystemCategoryBean bean) {
        retailBeanList = bean.getRetail();
        batchBeanList = bean.getBatch();

        ArrayAdapter retailAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, retailBeanList);
        retailAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        retailSystemCategorySp.setAdapter(retailAdapter);

        ArrayAdapter beanAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, batchBeanList);
        beanAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        batchSystemCategorySp.setAdapter(beanAdapter);
    }

    @OnClick({R.id.toolbar_left_btn, R.id.shop_base_info_commit, R.id.shop_base_info_service_region})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_left_btn:
                finish();
                break;
            case R.id.shop_base_info_commit:
                String shopName = shopNameEt.getText().toString().trim();
                String spokesman = spokesmanEt.getText().toString().trim();
                int sellerType = sellerTypes.get(sellerTypeSp.getSelectedItemPosition()).getId();
                //零售分类
                int retailSystemCategory = retailBeanList.get(retailSystemCategorySp.getSelectedItemPosition()).getId();
                //批发分类
                int batchSystemCategory = batchBeanList.get(batchSystemCategorySp.getSelectedItemPosition()).getId();
                String address = serviceRegionTv.getText().toString().trim();
                int isRetail = 0;//是否是零售
                int isWholesale = 0;//是否是批发
                if (isRetailCb.isChecked()) {
                    isRetail = 1;
                } else {
                    retailSystemCategory = 0;
                }
                if (isWholesaleCb.isChecked()) {
                    isWholesale = 1;
                } else {
                    batchSystemCategory = 0;
                }
                if (TextUtils.isEmpty(shopName)) {
                    showMessage("请填写店铺名字");
                    return;
                }
                if (TextUtils.isEmpty(spokesman)) {
                    showMessage("请填写法人名字");
                    return;
                }
                commitBean.setUser_id(SellerApp.userId);
                commitBean.setSeller_name(shopName);
                commitBean.setSpokesman(spokesman);
                commitBean.setSeller_type(sellerType);
                commitBean.setCategory_id(retailSystemCategory);
                commitBean.setBatch_category_id(batchSystemCategory);
                commitBean.setArea_tree(SPtools.getString(this, "province", "")+","+SPtools.getString(this, "city", "")+","+SPtools.getString(this, "district", ""));
                commitBean.setSelect_retail(isRetail);
                commitBean.setSelect_wholesale(isWholesale);
                Intent intent = new Intent(this, ShopAptitudeActivity.class);
                intent.putExtra("commit_bean", commitBean);
                startActivity(intent);
                break;
            case R.id.shop_base_info_service_region:
                Intent i = new Intent(this, SelectProvinceActivity.class);
                startActivity(i);
                break;
        }

    }
}
